package com.pda.yuanjing.pages.outbill.scanf;

import android.content.Context;
import com.apkfuns.logutils.LogUtils;
import com.pda.yuanjing.beanin.CodeBeanIn;
import com.pda.yuanjing.beanin.ScanfBeanIn;
import com.pda.yuanjing.beanout.ScanfBeanOut;
import com.pda.yuanjing.thread.CodeBeans;
import com.public_module.basemvp.model.BaseModel;
import com.public_module.config.SetConfig;
import com.public_module.tools.SharedPreferencesManger;
import com.public_module.tools.StringProcess;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Response;

public class OutScanfModelImpl extends BaseModel implements OutScanfModel {
    private String TAG = "InScanfModelImpl";
    private ScanfBeanOut scanfBeanOut;
    private ScanfBeanOut scanfBeanOutt;
    public OutScanfModelImpl(Context context) {
        super(context);
    }

    @Override
    public void getDeatail(String billID, final CallBack callBack) {
        try {
            ScanfBeanIn scanfBeanIn = new ScanfBeanIn();
            scanfBeanIn.setAction("DownloadBillListail");
            scanfBeanIn.setCid(SharedPreferencesManger.getCid());
            scanfBeanIn.setUserID(SharedPreferencesManger.getUid());
            scanfBeanIn.setStoreID(SharedPreferencesManger.getStoreID());
            scanfBeanIn.setBillID(billID);
            scanfBeanIn.setBillType("1");
            RequestBody body = RequestBody.create(SetConfig.JSON, gson.toJson(scanfBeanIn));
            api.downloadBillDeatail(body)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new io.reactivex.Observer<ResponseBody>() {
                        private Disposable mDisposable;

                        @Override
                        public void onSubscribe(Disposable d) {
                            mDisposable = d;
                        }

                        @Override
                        public void onComplete() {
                            callBack.oninitView(scanfBeanOut.getData());
                            mDisposable.dispose();
                        }

                        @Override
                        public void onError(Throwable e) {
                            if (scanfBeanOut != null && scanfBeanOut.getErrmsg() != null) {
                                callBack.onshowres(scanfBeanOut.getErrmsg());
                            } else {
                                callBack.onshowres(SetConfig.connectError);
                            }
                            mDisposable.dispose();
                        }

                        @Override
                        public void onNext(ResponseBody abBean) {
                            LogUtils.w(StringProcess.responsetoString(abBean));
                            scanfBeanOut = gson.fromJson(StringProcess.responsetoString(abBean), ScanfBeanOut.class);
                            if (scanfBeanOut != null && scanfBeanOut.getErrcode() == 0 &&  scanfBeanOut.getData().size() > 0) {
                                onComplete();
                            } else {
                                onError(new Throwable());
                            }
                        }
                    });
        } catch (Exception e) {
            callBack.onshowres(SetConfig.connectError);
            e.printStackTrace();
        }
    }

    @Override
    public void addCode(String billID, String billListID, String productID, CodeBeans codeBeans, boolean flag, final CallBack callBack) {
        try {
            scanfBeanOutt = new ScanfBeanOut();
            scanfBeanOutt.setErrcode(-1);
            scanfBeanOutt.setErrmsg(SetConfig.getConnectError());
            CodeBeanIn codeBeanIn = new CodeBeanIn();
            codeBeanIn.setAction("UpdateCodes");
            codeBeanIn.setCid(SharedPreferencesManger.getCid());
            codeBeanIn.setUserID(SharedPreferencesManger.getUid());
            codeBeanIn.setStoreID(SharedPreferencesManger.getStoreID());
            codeBeanIn.setBillID(billID);
            codeBeanIn.setBillListID(billListID);
            codeBeanIn.setProductID(productID);
            codeBeanIn.setBillType("1");
            codeBeanIn.setCode(codeBeans.getCodes().get(0).getStr());
            if(flag){
                codeBeanIn.setIsVirtualTuo("1");
            }else {
                codeBeanIn.setIsVirtualTuo("0");
            }
            RequestBody body = RequestBody.create(SetConfig.JSON, gson.toJson(codeBeanIn));
            final Call<ResponseBody> call = apisync.addCode(body);
            Response<ResponseBody> response = call.execute();
            if (response.isSuccessful()) {
                LogUtils.e(StringProcess.responsetoString(response.body()));
                scanfBeanOutt = gson.fromJson(StringProcess.responsetoString(response.body()), ScanfBeanOut.class);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            codeBeans.removeCode(codeBeans.getCodes().get(0));
            if(scanfBeanOutt.getErrcode()==0){
                callBack.onupdateView(scanfBeanOutt.getData());
            }else {
                callBack.onshowres(scanfBeanOutt.getErrmsg());
            }
        }
    }

    @Override
    public void deleteCode(String billID,String billListID,String productID, CodeBeans codeBeans, boolean flag,final CallBack callBack) {
        try {
            scanfBeanOutt = new ScanfBeanOut();
            scanfBeanOutt.setErrcode(-1);
            scanfBeanOutt.setErrmsg(SetConfig.getConnectError());
            CodeBeanIn codeBeanIn = new CodeBeanIn();
            codeBeanIn.setAction("DeleteCode");
            codeBeanIn.setCid(SharedPreferencesManger.getCid());
            codeBeanIn.setUserID(SharedPreferencesManger.getUid());
            codeBeanIn.setStoreID(SharedPreferencesManger.getStoreID());
            codeBeanIn.setBillID(billID);
            codeBeanIn.setBillListID(billListID);
            codeBeanIn.setProductID(productID);
            codeBeanIn.setBillType("1");
            codeBeanIn.setCode(codeBeans.getCodes().get(0).getStr());
            if(flag){
                codeBeanIn.setIsVirtualTuo("1");
            }else {
                codeBeanIn.setIsVirtualTuo("0");
            }
            RequestBody body = RequestBody.create(SetConfig.JSON, gson.toJson(codeBeanIn));
            final Call<ResponseBody> call = apisync.addCode(body);
            Response<ResponseBody> response = call.execute();
            if (response.isSuccessful()) {
                LogUtils.e(StringProcess.responsetoString(response.body()));
                scanfBeanOutt = gson.fromJson(StringProcess.responsetoString(response.body()), ScanfBeanOut.class);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            codeBeans.removeCode(codeBeans.getCodes().get(0));
            if(scanfBeanOutt.getErrcode()==0){
                callBack.onupdateView(scanfBeanOutt.getData());
            }else {
                callBack.onshowres(scanfBeanOutt.getErrmsg());
            }
        }
    }
}

